//
//  ClassifyDetailedViewController.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassifyDetailedViewController.h"
#import "AFHTTPSessionManager.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPRequestOperation.h"
#import "UIImageView+WebCache.h"


#import "MJRefresh.h"
#import "ClassifyModel.h"
#import "ClassifyDetailTabelViewCell.h"

#import "ClassifyDetail.h"

#import "CartoonDViewController.h"

#define HEIGHT  self.view.frame.size.height
#define WIDTH   self.view.frame.size.width

@interface ClassifyDetailedViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *classifyDetailArr;
@property(nonatomic,retain)UITableView *classifyDetailTabelView;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)BOOL firstData;
@property(nonatomic,assign)NSInteger page;

//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;

@end

@implementation ClassifyDetailedViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.classifyDetailArr = [NSMutableArray array];
        self.page = 0;
        
    }
    
    return self;
}

- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
}


- (void)createData
{
    self.page++;
    NSString *URL = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/local_version/2.3.2/channel/1001/guest_id/921199FA-97DD-457B-80FA-5A7E1D0FC7BA/uin/null/classify_id/%@/page/%ld/listcnt/12",self.classify_id,self.page];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *resDic = responseObject;
        NSMutableArray *arr = [resDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in arr) {
            ClassifyModel *model = [[ClassifyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.classifyDetailArr addObject:model];
            [model release];
       }
        [self.classifyDetailTabelView reloadData];
        NSLog(@"2请求数据成功2");
        NSLog(@"结束动画");
        [self.witeImageView stopAnimating];
        [self.view bringSubviewToFront:self.classifyDetailTabelView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求数据失败");
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self createAvtivity];
    [self createWiteImage];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [[UIColor alloc]initWithRed:119 / 256.0 green:99 / 256.0 blue:88 / 256.0 alpha:1.0];
    [self createView];
    [self createData];
    [self addFoooter];   // 加载
    [self addHeader];    // 刷新
    // 叠起的页面
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao-2"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    [self.witeImageView startAnimating];
}

    

- (void)createView
{
    
    self.classifyDetailTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT - 64 -49 ) style:UITableViewStylePlain];
    [self.view addSubview:self.classifyDetailTabelView];
    [_classifyDetailTabelView release];
    
    self.classifyDetailTabelView.backgroundColor = [UIColor whiteColor];
    CGFloat bili1 = 107 / 80.0;
    self.classifyDetailTabelView.rowHeight = WIDTH / 4.5 * bili1 + 20;
    
    self.classifyDetailTabelView.delegate = self;
    self.classifyDetailTabelView.dataSource = self;
    self.navigationController.navigationBar.translucent = NO;
    
 
   
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.classifyDetailArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ClassifyDetailTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[ClassifyDetailTabelViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    ClassifyModel *mo = self.classifyDetailArr[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:mo.cover_url]];
    cell.backgroundColor = [[UIColor alloc]initWithRed:119 / 256.0 green:99 / 256.0 blue:88 / 256.0 alpha:1.0];
    cell.typeLabel.text = [NSString stringWithFormat:@"类型: %@",mo.type];
    cell.titleLabel.text = mo.title;
    cell.grade_aveLabel.text = [NSString stringWithFormat:@"评分: %@",mo.grade_ave];
    cell.lastupLabel.text = [NSString stringWithFormat:@"最近更新: %@",mo.lastup];
    
    return cell;
}



- (void)addHeader
{
   
    // 下拉刷新
    
    [self.classifyDetailTabelView addHeaderWithCallback:^{
        // 解析数据
        self.page = 0;
        [self.classifyDetailArr removeAllObjects];
        [self createData];
        [self.classifyDetailTabelView reloadData];
        [self.classifyDetailTabelView headerEndRefreshing];
        
    }];
    
    [self.classifyDetailTabelView headerBeginRefreshing];

//    [self.classifyDetailTabelView headerBeginRefreshing];

}
    
 
- (void)addFoooter
{
    [self.classifyDetailTabelView addFooterWithCallback:^{
        // 解析数据
        [self createData];
        [self.classifyDetailTabelView reloadData];
        [self.classifyDetailTabelView footerEndRefreshing];
    }];
    
    
    
    
    
    
    
    
}


  // 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonDViewController *carToon = [[CartoonDViewController alloc] init];
      carToon.comic_id  = [self.classifyDetailArr[indexPath.row] comic_id];
    
    [self.navigationController pushViewController:carToon animated:YES];
    
    
}


//列表的按钮
- (void)rightBarAction:(id)sender
{
    
    ClassifyDetail *class = [[ClassifyDetail alloc] init];
    class.classify_id = self.classify_id;
    [self.navigationController pushViewController:class animated:YES];
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
